﻿var map = null;
var mapLayers = [];
dojo.require("esri.map");
dojo.require("dijit.layout.BorderContainer");
dojo.require("dijit.layout.ContentPane");

function InitializeMapScriptsDojo() {

    var initialExtent = new esri.geometry.Extent({ "xmin": -10647645.426992767, "ymin": 3855648.541384007, "xmax": -9875325.693199385, "ymax": 4444519.407292976, "spatialReference": { "wkid": 102100 } });
    //there can only be one map created per session so it is created in the head 
    map = new esri.Map("divmap", { extent: initialExtent });
    var streetsBaseMap = new esri.layers.ArcGISTiledMapServiceLayer("https://server.arcgisonline.com/ArcGIS/rest/services/World_Street_Map/MapServer", { id: "streets" });
    var aerialBaseMap = new esri.layers.ArcGISTiledMapServiceLayer("https://server.arcgisonline.com/ArcGIS/rest/services/World_Imagery/MapServer", { id: "aerial", visible: false });
    var topoBaseMap = new esri.layers.ArcGISTiledMapServiceLayer("https://server.arcgisonline.com/ArcGIS/rest/services/World_Topo_Map/MapServer", { id: "topo", visible: false });

    map.addLayer(streetsBaseMap);
    map.addLayer(aerialBaseMap);
    map.addLayer(topoBaseMap);

    locator = new esri.tasks.Locator("http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Locators/ESRI_Geocode_USA/GeocodeServer");
    dojo.connect(locator, "onAddressToLocationsComplete", showResults);

    dojo.connect(map, 'onLoad', function (theMap) {
        //resize the map when the browser resizes
        dojo.connect(dijit.byId('divmap'), 'resize', map, map.resize);
    });
    dojo.connect(map, 'onLayerAdd', function (layer) {
        console.log("Layer Added: " + layer.id);
        mapLayers.push(layer);
    });
    
    

}



function locateAddress() {
    var address = {
        Address: $("#street").val(),
        City: $("#city").val(),
        State: $("#state option:selected").val(),
        Zip: $("#zipcode").val()
    };
    locator.addressToLocations(address, ["Loc_name"]);
}

function showResults(candidates) {
    var candidate;
    var symbol = new esri.symbol.SimpleMarkerSymbol();
    var infoTemplate = new esri.InfoTemplate("${Name}", "Address: ${address} ");

    symbol.setStyle(esri.symbol.SimpleMarkerSymbol.STYLE_DIAMOND);
    symbol.setColor(new dojo.Color([255, 0, 0, 0.75]));

    var points = new esri.geometry.Multipoint(map.spatialReference);

    for (var i = 0, il = candidates.length; i < il; i++) {
        candidate = candidates[i];
        if (candidate.score > 70) {
            var attributes = { address: candidate.address, Name: $("#firstName").val() + " " + $("#lastName").val() };
            var graphic = new esri.Graphic(esri.geometry.geographicToWebMercator(candidate.location), symbol, attributes, infoTemplate);
            map.graphics.add(graphic);
            var textSymbol = new esri.symbol.TextSymbol(attributes.address).setOffset(0, 8).setFont(new esri.symbol.Font("14pt", esri.symbol.Font.STYLE_ITALIC, esri.symbol.Font.VARIANT_NORMAL,
     esri.symbol.Font.WEIGHT_BOLD, "Courier"));
            map.graphics.add(new esri.Graphic(esri.geometry.geographicToWebMercator(candidate.location), textSymbol));
            points.addPoint(esri.geometry.geographicToWebMercator(candidate.location));
        }
    }
    map.setExtent(points.getExtent().expand(3));

}


var mapClickHandle = null;
function turnOnCirclesOnMapClick() {
    turnOffMapClick();
    mapClickHandle = dojo.connect(map, "onClick", function (evt) {
        addExampleGraphicsLayer(evt, esri.symbol.SimpleMarkerSymbol.STYLE_CIRCLE);

    });
}

function turnOnCrossOnMapClick() {
    turnOffMapClick();
    mapClickHandle = dojo.connect(map, "onClick", function (evt) {
        addExampleGraphicsLayer(evt, esri.symbol.SimpleMarkerSymbol.STYLE_CROSS);
    });
}
function turnOnDiamondOnMapClick() {
    turnOffMapClick();
    mapClickHandle = dojo.connect(map, "onClick", function (evt) {
        addExampleGraphicsLayer(evt, esri.symbol.SimpleMarkerSymbol.STYLE_DIAMOND);
    });
}
function turnOnSquareOnMapClick() {
    turnOffMapClick();
    mapClickHandle = dojo.connect(map, "onClick", function (evt) {
        addExampleGraphicsLayer(evt, esri.symbol.SimpleMarkerSymbol.STYLE_SQUARE);
    });
}
function turnOnXOnMapClick() {
    turnOffMapClick();
    mapClickHandle = dojo.connect(map, "onClick", function (evt) {
        addExampleGraphicsLayer(evt, esri.symbol.SimpleMarkerSymbol.STYLE_X);
    });
}


function turnOffMapClick() {
    if (mapClickHandle != null) {
        dojo.disconnect(mapClickHandle);
        mapClickHandle = null;
    }

}

function addExampleGraphicsLayer(evt, symbolStyle) {
    var exampleLayer = new esri.layers.GraphicsLayer();

    //var geometry = new esri.geometry.Point([-91.72877545099999, 35.24958643800011], new esri.SpatialReference({ wkid: 4326 }));
    //geometry = esri.geometry.geographicToWebMercator(geometry);
    var geometry = evt.mapPoint;
    var symbol = new esri.symbol.SimpleMarkerSymbol();
    symbol.setStyle(symbolStyle);
    symbol.setColor(new dojo.Color([255, 0, 0, 1]));

    exampleLayer.add(new esri.Graphic(geometry, symbol));

    exampleLayer.setOpacity(.5);
    map.addLayer(exampleLayer);


    // alert("test");
}